package br.com.ordersys.web.services;

import java.util.Collection;

import org.springframework.stereotype.Service;

import br.com.ordersys.framework.restlayer.RestCall;
import br.com.ordersys.framework.restlayer.RestCallContext;
import br.com.ordersys.framework.restlayer.auth.AuthenticationData;
import br.com.ordersys.framework.scheduler.SchedulerManager;
import br.com.ordersys.framework.scheduler.SchedulerManager.RegisteredJob;
import br.com.ordersys.web.menu.Menu;
import br.com.ordersys.web.menu.MenuHelper;
import br.com.ordersys.web.services.input.JobDataInput;

@Service("sys")
public class SysServices implements ISysServices {

	
	@RestCall(inputTypeClass = JobDataInput.class)
	@Override
	public void stopJob() {
		JobDataInput input = (JobDataInput) RestCallContext.get().getInputObject();
		AuthenticationData auth = RestCallContext.get().getAuthenticationData();

		SchedulerManager.getInstance().stopJob(auth.getTenantID(), input.getJobid());
	}

	@RestCall(inputTypeClass = JobDataInput.class)
	@Override
	public void startJob() {
		JobDataInput input = (JobDataInput) RestCallContext.get().getInputObject();
		AuthenticationData auth = RestCallContext.get().getAuthenticationData();

		SchedulerManager.getInstance().refreshJob(auth.getTenantID(), input.getJobid());
	}

	@RestCall(inputTypeClass = JobDataInput.class)
	@Override
	public void listJobs() {
		Collection<RegisteredJob> rjs = SchedulerManager.getInstance().getRegisteredJobs(AuthenticationData.get().getTenantID());

		for (RegisteredJob rj : rjs) {
			System.out.println(rj);
		}
	}

	@RestCall
	@Override
	public Menu getSystemMenu() {
		MenuHelper mh = new MenuHelper();
		
		return mh.build();
	}
}
